/*
Copyright (C) 2009-2010 Xanadu Development Team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2
of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "../PacketCreator.h"
#include "../../server/Player.h"
#include "../../server/PlayerShop.h"

PacketWriter *PacketCreator::openPlayerShop(Player *player, PlayerShop *shop) {
	pw.writeShort(PLAYER_INTERACTION);
	pw.write(0x05);
	pw.write(0x04);
	pw.write(0x04);
	pw.write(!shop->isOwner(player->getID()));
	pw.write(0);
	addCharLook(shop->getOwner());
	pw.writeString(shop->getOwnerName());
	if (shop->getNumVisitors() != 0) {
		for (unsigned char i = 0; i < 3; i++) {
			Player *visitor = shop->getVisitor(i);
			if (visitor != 0) {
				pw.write(shop->getPlayerSlot(visitor->getID()));
				addCharLook(visitor);
				pw.writeString(visitor->getName());
			}
		}
	}
	pw.write(0xFF);
	pw.writeString(shop->getDescription());
	pw.write((shop->getItemId() % 10 == 0) ? 0x10 : 0x18);
	pw.write(shop->getNumItems());
	for (unsigned char i = 0; i < shop->getNumItems(); i++) {
		PlayerShopItem *sellItem = shop->getItem(i);
		pw.writeShort(sellItem->getBundles());
		pw.writeShort(sellItem->getQuantity());
		pw.writeInt(sellItem->getPrice());
		itemInfo(sellItem->getItem(), false);
	}
	return &pw;
}

PacketWriter *PacketCreator::roomChat(char slot, const string &chat) {
	pw.writeShort(PLAYER_INTERACTION);
	pw.write(0x06);
	pw.write(0x08);
	pw.write(slot);
	pw.writeString(chat);
	return &pw;
}

PacketWriter *PacketCreator::playerShopAddVisitor(Player *player, char slot) {
	pw.writeShort(PLAYER_INTERACTION);
	pw.write(0x04);
	pw.write(slot);
	addCharLook(player);
	pw.writeString(player->getName());
	return &pw;
}

PacketWriter* PacketCreator::playerShopRemoveVisitor(int slot) {
	pw.writeShort(PLAYER_INTERACTION);
	pw.write(0x0A);
	pw.write(slot);
	return &pw;
}

PacketWriter *PacketCreator::playerShopError(char type) {
	// 01 = You don't have enough in stock
	// 02 = You do not have enough mesos
	// 03 = The price of item is too high for the trade
	// 04 = The player does not posses enough money to trade anymore
	// 05 = Please check if your inventory is full or not
	// 06 = You cannot carry more than one for this item
	// 07 = Due to an error, the trade did not happen
	// 08 = Player below Level 15, 1 mil trade limit per day msg
	// 09+ = Due to an error, the trade did not happen
	pw.writeShort(PLAYER_INTERACTION);
	pw.write(0x18);
	pw.write(type);
	return &pw;
}

PacketWriter *PacketCreator::playerShopMessage(char type, char message) {
	// Type = 5
	// 02 = Shop full
	// 09 = Only make trade when both in map
	// 10 = Too close to portal
	// 11 = Cannot setup miniroom here
	// 17 = Banned
	// 18 = Shop maintenance
	pw.writeShort(PLAYER_INTERACTION);
	pw.write(type);
	pw.write(0x00);
	pw.write(message);
	return &pw;
}

PacketWriter *PacketCreator::playerShopEndMessage(char message, bool owner) {
	// 01 = Banned
	// 03 = Shop closed
	// 06 = Success
	// 07 = Unsuccessful
	// 08 = Cannot carry more then of that one item
	// 09 = Other person is on a different map
	// 10 = Items out of stock
	// 13 = Shop maintenance
	pw.writeShort(PLAYER_INTERACTION);
	pw.write(0x0A);
	pw.write(!owner);
	pw.write(message);
	return &pw;
}

PacketWriter *PacketCreator::playerShopUpdateItems(PlayerShop *shop) {
	pw.writeShort(PLAYER_INTERACTION);
	pw.write(0x19);
	pw.write(shop->getNumItems());
	for (unsigned char i = 0; i < shop->getNumItems(); i++) {
		PlayerShopItem *sellItem = shop->getItem(i);
		pw.writeShort(sellItem->getBundles());
		pw.writeShort(sellItem->getQuantity());
		pw.writeInt(sellItem->getPrice());
		itemInfo(sellItem->getItem(), false);
	}
	return &pw;
}

PacketWriter *PacketCreator::showPlayerShop(PlayerShop *shop) {
	pw.writeShort(MINIROOM_UPDATE);
	pw.writeInt(shop->getOwnerId());
	pw.write(0x04);
	pw.writeInt(shop->getId());
	pw.writeString(shop->getDescription());
	pw.write(0x00);
	pw.write(shop->getItemId() % 10);
	pw.write(0x01);
	pw.write((shop->getEmptyVisitorSlot() > -1) ? 4 : 1);
	pw.write(0x00);
	return &pw;
}

PacketWriter *PacketCreator::removePlayerShop(Player *player, Player *nonsender) {
	pw.writeShort(MINIROOM_UPDATE);
	pw.writeInt(player->getID());
	pw.write(0x00);
	return &pw;
}
